<?php defined('SYSPATH') or die('No direct script access.');

/**
 * redis队列处理类
 */
class Hooks_Enqueue_Queue
{
    /**
     * 入队操作
     * @param $queue
     * @param $class
     * @param $arg
     * @param int $task_id
     * @return bool
     * @throws Kohana_Exception
     * @date 2022/2/17 0017
     * @author gl
     */
    public static function handle($queue, $class, $arg, $task_id = 0)
    {
        Resque::setBackend('127.0.0.1:6379');
        $token = Resque::enqueue($queue, $class, $arg, true);

        if (empty($task_id)) {
            //插入任务表
            $data = array(
                'job_name'    => $class,
                'queue_name'  => $queue,
                'args'        => json_encode($arg),
                'token'       => $token,
                'status'      => 1,
                'create_time' => time(),
            );
            DB::insert('queue_job', array_keys($data))->values(array_values($data))->execute();
        }
        else {
            DB::update('queue_job')->set(array('token' => $token))
                ->where('id', '=', $task_id)->execute();
        }


        return true;
    }

    /**
     * 删除队列任务
     * @param $queue
     * @param $class
     * @date 2022/1/27 0027
     * @author gl
     */
    public static function del_job($queue, $class)
    {
        Resque::setBackend('localhost:6379');
        //删除任务
        Resque::dequeue($queue, [$class]);

    }
}